<?php
/**
 * Minhlog 脚本功能控制器
 *
 * LICENSE
 *
 * This source file is subject to the new BSD license that is bundled
 * with this package in the file LICENSE.txt.
 *
 * @category   Controller
 * @package    Frontend
 * @copyright  Copyright (c) 2009-2010 Minh.Shu@gmail.com
 * @license    New BSD License
 * @author     Minh.Shu
 * @version    $Id: ScriptController.php 2 2010-01-17 13:49:08Z Minh.Shu $
 *
 */

class ScriptController extends Minh_Controller_Action
{
    protected $_articleModel;

    public function init()
    {
        if(null === $this->_articleModel){
            if(class_exists('Domain_Model_Article')){
                $this->_articleModel = new Domain_Model_Article();
            } else {
                throw new Exception('文章模型类不存在!');
            }
        }

        parent::init();
    }

    public function createcaptchaAction ()
    {
        $captcha = new Minh_Captcha_Image(array('wordLen' => rand(4, 5) , 'fontsize' => 12 , 'width' => 50 , 'height' => 20 , 'dotNoiseLevel' => 3 , 'lineNoiseLevel' => 2 , 'font' => './public/font/consola.ttf' , 'imgDir' => './public/captcha/' , 'imgUrl' => '/public/captcha/'));
        $captcha->setGcFreq(3);
        $captcha->generate();
        $image = '.'.$captcha->getImgUrl() . $captcha->getId(). $captcha->getSuffix();    //图片相对路径
        $_SESSION['captcha']['captchValue'] = md5(crypt($captcha->getWord(), substr($captcha->getWord(), 0, 2))); //获取当前生成的验证字符串
        header("Pragma: no-cache");
        header("Cache-control: no-cache");
        header("Content-Type:image/png");
        $im = imagecreatefrompng($image);
        imagepng($im);
    }

    public function statAction ()
    {
        $filter = new Zend_Filter_Int();
        $articleId = $filter->filter($this->_request->getParam('id'));
        //文章计数统计
        $statNamespace = new Zend_Session_Namespace('stat');
        $statNamespace->articleId = $articleId;
        if (isset($statNamespace->articleId)) {
            $statNamespace->$articleId ++; // 每次页面加载，这个将递增
        } else {
            $statNamespace->$articleId = 1; // 第一次
        }
        if ($statNamespace->$articleId == 1) {
            $articleData = array(
                    'click_count' =>  new Zend_Db_Expr('click_count+1')
            );
            $this->_articleModel->saveArticle($articleData, $articleId);
        }
    }
}